<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="餐台区域列表"
      row-key="id"
      :columns="columns"
      :request-api="getFoodDeskCategoryList"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增')"> 创建区域 </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'query'" type="primary" link :icon="View" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <RegionDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="ts" name="tableArea">
import { ref } from "vue";
import { Restaurant } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import RegionDialog from "@/views/restaurant/components/RegionDialog.vue";
import { CirclePlus, Delete, EditPen, View } from "@element-plus/icons-vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import {
  getFoodDeskCategoryList,
  createFoodDeskCategory,
  deleteFoodDeskCategory,
  editFoodDeskCategory
} from "@/api/modules/restaurant";

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
// const initParam = reactive({ storeId: 0 });

const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};

// 表格配置项
const columns: ColumnProps<Restaurant.ResFoodDeskCategoryList>[] = [
  { type: "index", label: "序号", width: 60 },
  { prop: "name", label: "区域名称", align: "left", minWidth: 120, sortable: true, search: { el: "input" } },
  { prop: "deskCount", label: "台桌数量", align: "left", sortable: true, minWidth: 120 },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

// 删除
const deleteAccount = async (params: Restaurant.ResFoodDeskCategoryList) => {
  await useHandleData(deleteFoodDeskCategory, { id: params.id }, `删除【${params.name}】区域`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof RegionDialog> | null>(null);
const openDialog = (title: string, row: Partial<Restaurant.ResFoodDeskCategoryList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createFoodDeskCategory : title === "编辑" ? editFoodDeskCategory : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
